import { type ListResponse, request } from '../http';
import type { IAvailableCoupon } from './restaurant-booking/interfaces/coupons';

export type CreatePreorderPayload = {
  puid: string; // 酒店ID
  checkin_at: string; // 入住日期
  checkout_at: string; // 离店日期
  roomItems: Partial<{
    room_id: string; // 房间ID
    room_no_id: string; // 房间号ID
    roomNum: number; // 房间数量
    adult: number; // 成人数量
    child: number; // 儿童数量
    infant: number; // 婴儿数量
    booking_fee: string; // 预定费用
    remark: string; // 备注
  }>[]; // 房间信息
  isLease: number; // 是否短租 1: 民宿 2: 短租
};

export function createPreorder(payload: Partial<CreatePreorderPayload>) {
  return request.post('/api/home/createPreOrder', payload);
}

export function getPreorderDetails(preOrderSn: string) {
  return request.post('/api/home/preOrderDetail', { preOrderSn });
}

type SelectDiscountPayload = {
  preOrderSn: string;
  isBalance?: number;
  couponId?: number;
};

export function selectDiscount(payload: SelectDiscountPayload) {
  request.options.loadingText = '正在设置';
  return request.post('/api/home/payInfo', payload);
}

type CreateOrderPayload = {
  preOrderSn: string;
  isBalance: number;
  couponId?: string;
  remark?: string;
  user: {
    first_name: string;
    last_name: string;
    email: string;
    phone: string;
    area_no: string;
  };
};

export function createOrder(payload: CreateOrderPayload) {
  request.options.loadingText = '请稍后...';
  return request.post('/api/home/createOrder', payload);
}

type ListPayload = {
  pageNum: number;
  pageSize: number;
};

export function getOrdersList(payload: ListPayload) {
  return request.post('/api/home/OrderList', payload);
}

export function getOrderDetail(orderSn: string) {
  return request.post('/api/home/OrderDetail', { orderSn });
}

export function cancelUnpaidOrder(orderSn: string) {
  request.options.loadingText = '取消中...';
  return request.post('/api/home/CancelOrder', { orderSn });
}

export function getOrderRefundDetails(orderSn: string) {
  return request.post('/api/home/refundOrderDetail', { orderSn });
}

export function getOrderPrerefundDetails(orderSn: string) {
  return request.post('/api/home/cancelOrderDetail', { orderSn });
}

export function cancelPaidOrder(orderSn: string, preCancelOrderSn: string) {
  request.options.loadingText = '取消中...';
  return request.post('/api/home/cancelOrder', { orderSn, preCancelOrderSn });
}

type GetOrderCouponsListPayload = {
  preOrderSn: string;
  pagination: number;
  pageNum: number;
  pageSize: number;
  scene:Number;
};

export function getOrderCouponsList(payload: GetOrderCouponsListPayload) {
  return request.post<ListResponse<IAvailableCoupon>>('/api/orderCouponList', payload);
}
